package com.tkc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tkc.common.ErrorCode;
import com.tkc.common.enums.BaseResultCodeEnum;
import com.tkc.common.result.CommonResult;
import com.tkc.common.result.ListResult;
import com.tkc.common.result.PageListResult;
import com.tkc.common.result.SingleResult;
import com.tkc.common.result.info.PageInfo;
import com.tkc.common.result.info.PerformanceInfo;
import com.tkc.common.util.Paging;
import com.tkc.controller.model.param.TeacherParam;
import com.tkc.controller.model.vo.TeacherVO;
import com.tkc.controller.util.ControllerErrorProcessor;
import com.tkc.converter.InfoConvertToVO;
import com.tkc.converter.ParamConvertToDTO;
import com.tkc.service.TeacherServices;
import com.tkc.service.model.dto.TeacherDTO;
import com.tkc.service.model.info.TeacherInfo;

/**
 * 
 * @Function: 资源对象为教师的相关操作的控制器
 * 
 * @Project :assessmenttask 
 * @Package :com.tkc.controller 
 * @File :TeacherController.java 
 * @ClassName: TeacherController  
 * 
 * @author tankaichao 
 * @email cgztkc@126.com 
 * @Date 2016年8月9日下午6:02:23
 * @since JDK 1.8 
 *
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController extends BaseController{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private TeacherServices teacherServices;
	
	/**
	 * 1.1.新增教师信息
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/register",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult addTeacher(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002,ErrorCode.ERR_CODE_20003,ErrorCode.ERR_CODE_20004,ErrorCode.ERR_CODE_20005,ErrorCode.ERR_CODE_20006,ErrorCode.ERR_CODE_20007);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		int values = teacherServices.addTeacher(teacherDTO);
		SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.info("新增教师信息成功");
		return result;
	}
	
	/**
	 * 1.2.查询教师信息
	 * @param teacherParam
	 * @param pageInfo
	 * @param orderBy
	 * @return
	 */
	@RequestMapping(value = "",method = RequestMethod.GET,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult getTeachers(TeacherParam teacherParam, PageInfo pageInfo, String orderBy) {
		pageInfo.setOffset((pageInfo.getCurPage() - 1) * pageInfo.getItemsPerPage());
		Sort sort = Paging.getPageSort(orderBy);
		pageInfo.setSort(sort);
		List<TeacherInfo> teacherInfos = teacherServices
				.selectTeacherByPage(ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam), pageInfo);
		List<TeacherVO> teacherVOs = InfoConvertToVO.convertFromTeacherInfosToTeacherVOs(teacherInfos);
		PageListResult<TeacherVO> result = new PageListResult<TeacherVO>(BaseResultCodeEnum.ERR_CODE_200, teacherVOs, pageInfo);
		logger.debug("查看教师信息成功");
		return result;
	}
	
	/**
	 * 1.3.修改教师信息
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.PUT,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult updateTeacher(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList, ErrorCode.ERR_CODE_20000);
		int values = teacherServices.updateTeacher(ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam));
		SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.info("修改教师信息成功");
		return result;
	}
	
	
	/**
	 * 1.4.删除教师信息
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/remove",method = RequestMethod.DELETE,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult deleteTeacher(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList, ErrorCode.ERR_CODE_20000);
		int values = teacherServices.deleteTeacher(ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam));
		SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.info("删除教师信息成功");
		return result;
	}
	
	
	/**
	 * 2.1.逐条新增千万级别条教师信息
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/register/onetoone",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult addCroreTeachersOneToOne(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002,ErrorCode.ERR_CODE_20003,ErrorCode.ERR_CODE_20004,ErrorCode.ERR_CODE_20005,ErrorCode.ERR_CODE_20006,ErrorCode.ERR_CODE_20007);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		PerformanceInfo performanceInfo = teacherServices.addCroreTeacherOneToOne(teacherDTO);
	    List<PerformanceInfo> list = new ArrayList<PerformanceInfo>();
	    list.add(performanceInfo);
	    ListResult<PerformanceInfo> result = new ListResult<PerformanceInfo>(BaseResultCodeEnum.ERR_CODE_200, list);
		logger.info("逐条新增千万级别条教师信息成功");
		return result;
	}
	
	
	/**
	 * 2.2.批量新增千万级别条教师信息
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/register/bulk",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult addCroreTeachersBulk(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002,ErrorCode.ERR_CODE_20003,ErrorCode.ERR_CODE_20004,ErrorCode.ERR_CODE_20005,ErrorCode.ERR_CODE_20006,ErrorCode.ERR_CODE_20007);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		PerformanceInfo performanceInfo = teacherServices.addCroreTeacherBulk(teacherDTO);
	    List<PerformanceInfo> list = new ArrayList<PerformanceInfo>();
	    list.add(performanceInfo);
	    ListResult<PerformanceInfo> result = new ListResult<PerformanceInfo>(BaseResultCodeEnum.ERR_CODE_200, list);
		logger.info("批量新增千万级别条教师信息成功");
		return result;
	}
	
	/**
	 * 2.3.存储过程新增千万级别条教师信息
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/register/storedprocedure",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult addCroreTeachersStoredprocedure(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002,ErrorCode.ERR_CODE_20003,ErrorCode.ERR_CODE_20004,ErrorCode.ERR_CODE_20005,ErrorCode.ERR_CODE_20006,ErrorCode.ERR_CODE_20007);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		PerformanceInfo performanceInfo = teacherServices.addCroreTeacherStoredprocedure(teacherDTO);
		List<PerformanceInfo> list = new ArrayList<PerformanceInfo>();
	    list.add(performanceInfo);
	    ListResult<PerformanceInfo> result = new ListResult<PerformanceInfo>(BaseResultCodeEnum.ERR_CODE_200, list);		
		logger.info("存储过程新增千万级别条教师信息成功");
		return result;
	}
	
	/**
	 * 2.4.往redis插入用户的用户名和密码
	 * @param teacherParam
	 * @param resultList
	 */
	@RequestMapping(value = "/redisregister",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult redisRegister(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		PerformanceInfo performanceInfo = teacherServices.addCroreTeacherToRedis(teacherDTO);
	    List<PerformanceInfo> list = new ArrayList<PerformanceInfo>();
	    list.add(performanceInfo);
	    ListResult<PerformanceInfo> result = new ListResult<PerformanceInfo>(BaseResultCodeEnum.ERR_CODE_200, list);
		logger.info("往Redis中新增千万级别条教师登录信息成功");
		return result;
	}
	
	/**
	 * 2.10.清空所有数据
	 * @return
	 */
	@RequestMapping(value = "/clear",method = RequestMethod.DELETE)
	@ResponseBody
	public CommonResult clearTeachers() {
		int values = teacherServices.clearTeacher();
		SingleResult<Integer> result = new SingleResult<Integer>(BaseResultCodeEnum.ERR_CODE_200, values);
		logger.info("清空教师信息成功");
		return result;
	}
	
	
	/**
	 * 3.1.登录校验
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/login",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult login(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		Map<String, Object> authInfo = teacherServices.selectTeacherByAuth(teacherDTO);
		SingleResult<Map<String,Object>> result = new SingleResult<Map<String,Object>>(BaseResultCodeEnum.ERR_CODE_200, authInfo);
		logger.debug("登录信息校验成功！");
		return result;
	}
	
	/**
	 * 3.2.Redis登录校验
	 * @param teacherParam
	 * @param resultList
	 * @return
	 */
	@RequestMapping(value = "/redisLogin",method = RequestMethod.POST,headers={"Accept=application/json"},consumes={"application/json"},produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public CommonResult redisLogin(@RequestBody @Valid TeacherParam teacherParam, BindingResult resultList) {
		ControllerErrorProcessor.paramErrorProcessor(resultList,ErrorCode.ERR_CODE_20001,ErrorCode.ERR_CODE_20002);
		TeacherDTO teacherDTO = ParamConvertToDTO.convertFromTeacherParamToTeacherDTO(teacherParam);
		Map<String, Object> authInfo = teacherServices.selectTeacherByRedisAuth(teacherDTO);
		SingleResult<Map<String,Object>> result = new SingleResult<Map<String,Object>>(BaseResultCodeEnum.ERR_CODE_200, authInfo);
		logger.debug("登录信息校验成功！");
		return result;
	}
	
	
	
	
}
